package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.w3c.dom.Document;

import persistence.CheckAndRegister;

/**
 * Servlet implementation class TestUserName
 */
//@WebServlet("/TestUserName")
public class TestUserName extends MyHttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	private void process(HttpServletRequest request,
			HttpServletResponse response) {
		Document xmlDoc = getDocument(request);
		String username = xmlDoc.getElementsByTagName("username").item(0).getFirstChild().getNodeValue();

		try {
			printWriter = response.getWriter();
			response.setContentType("application/xml");
			if (isAllowed(username)) {
				addTag("result", "okay");
			} else {
				addTag("result", "denied");
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	private boolean isAllowed(String username) {
		return CheckAndRegister.unregisteredName(username);
	}
}
